Web Application with Appache

Course- C IN LINUX >

In real life few web administrators would dream of letting anyone run C programs as CGI content generators because of the risk of crashes and core dumps. However the Apache server is itself written in C and there are simple utilities that come with its development tools that permit you to create code stubs into which you can place your C programs and run them as Apache modules when they are loaded as part of the server and managed safely in a kind of “sand-box”. Here we will take an earlier example and turn it into an Apache module.

A utility called apxs2 is included in the Apache2 development libraries which can be invoked to generate a code stub for a program which can be compiled into a module that is loaded and managed by the Apache web server. hese modules can be used to perform a huge variety of tasks but in our case we will do something which is akin the an ISAPI DLL found in the IIS server. he exact location of the apxs2 utility will change according to the Linux distribution you are using but with OpenSuse it runs like this.

In a terminal type:apxs2 -n labelmaker -g

This creates a folder of the name you give it (labelmaker) and a Makeile, a modules.mk ile which can be used by the Make utility, and a ile called mod_labelmaker.c.

This creates a folder of the name you give it (labelmaker) and a Makeile, a modules.mk ile which can be used by the Make utility, and a ile called mod_labelmaker.c.

The C file generated is kind of like a Hello World for Apache. It may look like a complex thing but it does supply a long explanatory comment header which is worth reading. he idea is that when Apache starts any modules in a speciied location which are conigured as needing to be loaded in the server coniguration iles, will be loaded. he *_register_hooks function lists the names and signatures of functions that can be called at speciic stages in the Apache server process. In this case if the name http://localhost/labelmaker is called this module will be asked to handle whatever happens in the *_handler function.

The coniguration of the server can be a bit iddly but in OpenSuse we have to add this to the file /etc/apache2/sites-available/default

<Location /labelmaker>
SetHandler labelmaker
</Location>
                
                

and in /etc/conig.sys/apache2 we add the name of our module labelmaker to long comma-separated list in the line starting

APACHE_MODULES=”…..,labelmaker”

Now go to the folder labelmaker and type:

sudo apxs2 -c -i mod_labelmaker.c sudo /etc/init.d/apache2 restart

Call this in a browser like this:

we add the name of our module labelmaker to long comma-separated list in the line starting
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"

/*Teh sample content handler*/

static int labelmaker_handler(request_rec *r)

{
 if(strcmp(r->handler, "labelmaker")){
 return DECLINED;

  }
 r->content_type="text/html";

 if(!r->header_only)
	ap_rputs("The sample page from mod_labelmaker.c\c", r);
	return OK;

}


static void labelmaker_register_hooks(apr_pool_t *p)

{
 ap_hook_handler(lablemaker_handler, NULL, NULL, APR_HOOK_MIDDLE);

}

/*Dispatch list for API hooks */
module AP-MODULE_DECLARE_DATA labelmaker_module={

	STANDERD20-MODULE_STUFF,
	NULL, 	/*create per-dir config structures*/
	NULL, 	/*create per-dir config structures*/
	NULL, 	/*create per-dir config structures*/
	NULL, 	/*create per-server config structures*/
	NULL, 	/*table of config file commands*/
	labelmaker_register_hooks /*register hooks*/
};